<template>
	<view>
		 <view class="palyer">
		 	<u--form labelPosition="left" :model="model" :rules="rules" ref="form1" labelWidth='80' errorType="toast" :labelStyle='formLabelStyle'>
		 		<u-form-item label="机构名称" prop="userInfo.institutionName" borderBottom ref="item1">
		 			<u--input v-model="model.userInfo.institutionName" border="none" placeholder="请输入机构名称"></u--input>
		 		</u-form-item>
		
		 		
		 		<u-form-item label="联系方式" prop="userInfo.institutionPhone" borderBottom ref="item1">
		 			<u--input v-model="model.userInfo.institutionPhone" placeholder="请输入手机号" border="none"></u--input>
		 		</u-form-item>
				<u-form-item label="省市区街道" prop="userInfo.areaAddress" borderBottom ref="item1" @click='ismask = true'>
					<u--input v-model="model.userInfo.areaAddress" disabled disabledColor="$bg-color" placeholder="请输入省市区街道" border="none"></u--input>
				</u-form-item>
				<u-form-item label="详细地址" prop="userInfo.detailAddress" borderBottom ref="item1">
					<u--input v-model="model.userInfo.detailAddress" placeholder="请输入详细地址" border="none"></u--input>
				</u-form-item>
		 		<u-form-item label="营业执照" prop="userInfo.businessLicense" borderBottom ref="item1"
		 			style='justify-content: space-between;'>
		 		
		 			<view style="flex:3"> </view>
		 			<uploadImage :fileList="fileList1" maxCount="1" @deletePic='deletePic' @afterRead='afterRead' />
		 		
		 		
		 		</u-form-item>
				<button class='submites' @click="btnSubmit">确定</button>
		 	</u--form>
		    <!-- 选择所在区域组件 -->
		    <choose-address :ismask="ismask" @transRegionInfo="transRegionInfo" @closePupop="closePupop"></choose-address>
		 </view>
	</view>
</template>

<script>
	// import uploadImage from '../../../components/uploadImage/uploadImage.vue'
	// import chooseAddress  from '@/components/choose-address/choose-address.vue'
	import {getCompetititonListApi,getInstitutionApplyApi} from '@/config/api.js'
	export default {
		components:{
			// uploadImage,
			// chooseAddress
		},
		data() {
			return {
				model:{
				userInfo:{
					institutionName:'',
					institutionPhone:'',
					areaAddress:'',
					detailAddress:'',
					businessLicense:[],
					provinceId:'',
					cityId:'',
					districtId:'',
					townId:'',
					competitionId:'',
				}},
				rules: {
					'userInfo.institutionName': {
						type: 'string',
						required: true,
						message: '请填写机构名称',
						trigger: ['blur', 'change']
					},
					'userInfo.institutionPhone': {
						type: 'string',
						required: true,
						message: '请输入联系方式',
						trigger: ['blur', 'change']
					},
					'userInfo.areaAddress': {
						type: 'string',
						required: true,
						message: '请输入区域地址',
						trigger: ['blur', 'change']
					},
					'userInfo.detailAddress': {
						type: 'string',
						required: true,
						message: '请输入详细地址',
						trigger: ['blur', 'change']
					},
					'userInfo.businessLicense': {
						type: 'array',
						max: 1,
						required: true,
						message: '请输入上传营业执照',
						trigger: ['blur', 'change']
					},
				},
				fileList1:[],
				ismask:false,
				formLabelStyle:this.$store.state.formLabelStyle
				
			}
		},
		onLoad(option) {
			if(option.competitionId){
				this.model.userInfo.competitionId = option.competitionId
			}
			this.$checkLogin(1).then(res=>{
			})
		   // getCompetititonListApi().then(res=>{
			  //  console.log(res)
			  //  this.model.userInfo.competitionId = res.records[0].id
		   // })	
		},
		onShow() {
			uni.hideHomeButton()
		},
		methods: {
			afterRead(e) {
				let obj = {}
				obj.url = e[1].url
				this.fileList1.push(obj)
				this.model.userInfo.businessLicense.push(e[1].url)
				
			},
			deletePic(e){
				this.fileList1.splice(e.index,1)
				this.model.userInfo.businessLicense.splice(e.index,1)
			},
			// 关闭弹出框
			closePupop(val) {
				this.ismask = val;
			},
			transRegionInfo(val) {
				this.reginInfo = val;
				this.ismask = this.reginInfo.ismask;
				this.model.userInfo.areaAddress = this.reginInfo.result;
				this.model.userInfo.provinceId = val.provinceId;
				this.model.userInfo.cityId = val.cityId;
				this.model.userInfo.districtId = val.districtId;
				this.model.userInfo.townId = val.townId;
			},
			btnSubmit() {
			     this.$refs.form1.validate().then(res => {
					 if(!uni.$u.test.mobile(this.model.userInfo.institutionPhone)){
						 uni.$u.toast('请输入正确手机号')
						 return
					 }
					 let data = {
						 ...this.model.userInfo,
						 businessLicense:this.model.userInfo.businessLicense.toString()
					 }
					 getInstitutionApplyApi(data).then(res=>{
						 console.log(res)
						 uni.$u.toast('申请成功')
						 if(this.model.userInfo.competitionId){
						 	uni.redirectTo({
						 		url:'/pages/home/home/homeDetail?id=' +this.model.userInfo.competitionId
						 	})
						 	return
						 }
						 uni.navigateBack({
						 	delta:1,
							fail(){
								uni.reLaunch({
									url:'/pages/home/home/home'
								})
							}
						 })
					 })
					}).catch(errors => {
						// uni.$u.toast('校验失败')
				 })
			},
		}
	}
</script>

<style scoped lang="scss">
	.palyer {
		padding: 0 20px;
		box-sizing: border-box;
	}
</style>
